#!/usr/bin/env bash

# ----------------------------------------------------------------------
# Filename:   systemManager-motd.sh
# Version:    1.0
# Date:       2021/03/30
# Author:     laiyixin
# Email:      laiyixin@kylinos.com.cn
# History：
#             Version 1.0, 2021/03/30
# Function:   systemManager-motd - 将登录信息设置为空
# Out:
#              0=> Success
#              1=> Fail
#              other=> TCONF
# ----------------------------------------------------------------------

## TODO: 使用ctrl+c退出
##
systemManagerOnCtrlC() {
    echo "正在优雅的退出..."
    systemManagerClean

    exit ${TFAIL}
}

## TODO: 用户界面
##
systemManagerUSAGE() {
    cat >&1 <<EOF
--------- systemManager-motd - 将登录信息设置为空 ---------
EOF
}

## TODO : 执行前的初始化
## Out  :
##        0=> Success
##        1=> Fail
##        other=> TCONF
systemManagerInit() {
    # 判断root用户
    if [ $(id -u) -ne 0 ]; then
        echo "Must use root ！"
        exit ${TCONF}
    fi

    # 信号捕获ctrl+c
    trap '  systemManagerOnCtrlC' INT

    systemManagerClean

}

## TODO : 执行进行时的方法
## Out  :
##        0=> Success
##        1=> Fail
##        other=> TCONF
systemManagerTest() {
    echo >/etc/motd
    systemManagerRetParse "null to /etc/motd"
}

## TODO : 执行收尾清除工作
##
systemManagerClean() {
    echo "Clean ..."

}

## TODO: 解析函数返回值
## In  : $1 => log
systemManagerRetParse() {
    local ret=$?
    local logstr=""

    if [ $# -eq 1 ]; then
        logstr="$1"
    fi

    if [ $ret -eq 0 ]; then
        echo "[pass] : ${logstr}"
    else
        echo "[fail] : ${logstr}"
        systemManagerClean
        exit ${TFAIL}
    fi
}

## TODO : Main
## Out  :
##        0=> Success
##        1=> Fail
##        other=> TCONF
systemManagerMain() {
    systemManagerUSAGE

    systemManagerInit

    systemManagerTest

    systemManagerClean

    return ${TPASS}
}

systemManagerMain
exit $?
